package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallActivityShare;
import com.sc.nft.entity.MallMember;
import com.sc.nft.entity.MallMemberEquity;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.*;
import com.sc.nft.enums.MallBillStatusEnum;
import com.sc.nft.mapper.MallMemberMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 商城会员Dao
 *
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：MallMemberDao
 * @Date：2024/4/2 19:27
 * @Filename：MallMemberDao
 */
@Slf4j
@Repository
public class MallMemberDao extends ServiceImpl<MallMemberMapper, MallMember> {

    /**
     * 获取商城会员
     *
     * @param memberLv 会员等级
     * @return
     */
    public MallMember getByMemberLv(Integer memberLv, Long id) {
        LambdaQueryWrapper<MallMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallMember::getMemberLv, memberLv);
        wrapper.ne(Objects.nonNull(id), MallMember::getId, id);
        wrapper.orderByDesc(MallMember::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }


    /**
     * 获取商城会员列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<AdminMallMemberPageDTO> getAdminMallMemberPageDTO(Integer pageNo, Integer pageSize) {
        return baseMapper.getAdminMallMemberPageDTO(new Page(pageNo, pageSize));
    }

    /**
     * 获取商城会员列表(app)
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<MallMemberPageDTO> getMallMemberPageDTO(Integer pageNo, Integer pageSize) {
        return baseMapper.getMallMemberPageDTO(new Page(pageNo, pageSize));
    }

    /**
     * 获取用户商城会员
     *
     * @param userId
     * @return
     */
    public List<UserMallMemberDTO> getUserMallMember(Long userId) {
        return baseMapper.getUserMallMember(userId);
    }

    /**
     * 修改商城会员状态
     *
     * @param id
     * @param status
     * @return
     */
    public boolean updateStatus(Long id, Boolean status) {
        UpdateWrapper<MallMember> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        return update(new MallMember(), wrapper);
    }

    /**
     * 修改商城会员排序
     *
     * @param id
     * @param sort
     * @return
     */
    public boolean updateSort(Long id, Integer sort) {
        UpdateWrapper<MallMember> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("sort", sort);
        return update(new MallMember(), wrapper);
    }

    public AdminMallMemberDetailDTO getAdminMallMemberDetailDTO(Long id) {
        return baseMapper.getAdminMallMemberDetailDTO(id);
    }

    public MallMember getByCollectionId(Long collectionId) {
        LambdaQueryWrapper<MallMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallMember::getCollectionId, collectionId);
        wrapper.eq(MallMember::getStatus, Boolean.TRUE);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }


}
